#!/usr/bin/env python3.10
# -*- encoding: utf-8 -*-
'''
@File    :   models.py
@Time    :   2024/02/19 15:53:12
@Author  :   windstarry 
@Version :   1.0
'''
# here put the import lib

from django.db import models
from django.contrib.auth.models import AbstractUser
from core.models import CoreModel


class Role(CoreModel):
    name = models.CharField(max_length=64, help_text='角色名称')
    tag = models.CharField(max_length=64, unique=True, help_text='标签')
  
    class Meta:
        db_table = 'system_role'
        verbose_name = "角色管理"
        verbose_name_plural = verbose_name
        ordering = ("id",)
        
    def __str__(self) -> str:
        return self.name


class Users(AbstractUser, CoreModel):
    username = models.CharField(
        max_length=150,
        unique=True,
        db_index=True,
        verbose_name="用户账号",
        help_text="用户账号",
    )
    email = models.EmailField(
        max_length=255, verbose_name="用户邮箱", null=True, blank=True, help_text="用户邮箱"
    ) 
    avatar  = models.ImageField(upload_to="avatars/", verbose_name="用户头像", default="avatars/default.jpg",null=True, blank=True, help_text="用户头像")
    nickname = models.CharField(max_length=40, null=True, blank=True,verbose_name="用户昵称", help_text="用户昵称")
    telephone = models.CharField(max_length=11, blank=True, null=True, verbose_name="手机号码",help_text='手机号码')
    status = models.BooleanField(default=True, verbose_name="状态", help_text="状态")
    GENDER_CHOICES = (
        (0, "未知"),
        (1, "男"),
        (2, "女"),
    )
    gender = models.IntegerField(
        choices=GENDER_CHOICES,
        default=0,
        verbose_name="性别",
        null=True,
        blank=True,
        help_text="性别",
    )
    # db_constraint=False 关闭数据库约束，保留关联查询,默认0为无角色
    role = models.ForeignKey(Role, on_delete=models.SET_NULL, null=True, blank=True, db_constraint=False, help_text='用户角色')

    class Meta:
        db_table = "system_users"
        verbose_name = "用户管理"
        verbose_name_plural = verbose_name
        ordering = ("id",)

    def __str__(self) -> str:
        return self.username
    
    
  
class ClassModel(CoreModel):
    name = models.CharField(max_length=100, help_text="班级名称")
    tag = models.CharField(max_length=100, unique=True, help_text="标签")
    users = models.ManyToManyField(Users, related_name='classes', help_text="班级成员")
    
    class Meta:
        db_table = 'system_class'
        verbose_name = "班级管理"
        verbose_name_plural = verbose_name
        ordering = ("id",)
        
    def __str__(self) -> str:
        return self.name

